/*
 *  $Id: Mana.cs 1054 2010-05-19 11:31:37Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Affects {
    /// <summary>
    /// Эффект: источник энергии (маны)
    /// Параметры: тип энергии и количество
    /// </summary>
    public class ManaIncome : Affect {
        public readonly ElementType SourceElement;
        public readonly int Value;

        public ManaIncome(ElementType element, int value) {
            SourceElement = element;
            Value = value;
        }

        public override string Id {
            get { return "mana.income"; }
        }
        public override string NameText {
            get { return Name.GetText(SourceElement, Value); }
        }

        public override ElementType Element {
            get { return SourceElement; }
        }

        public override int BaseWeight {
            get { return Value * 50; }
        }

        public override Affect Clone() {
            return new ManaIncome(SourceElement, Value);
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.ChangeManaIncome(SourceElement, Value);
            GlobalSummary.ChangeManaIncome(SourceElement, Value);
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.ChangeManaIncome(SourceElement, -Value);
            GlobalSummary.ChangeManaIncome(SourceElement, -Value);
        }
    }

    /// <summary>
    /// Эффект: расход энергии (маны)
    /// Параметры: тип энергии и количество
    /// </summary>
    public class ManaOutcome : Affect {
        public readonly ElementType SourceElement;
        public readonly int Value;

        public ManaOutcome(ElementType element, int value) {
            SourceElement = element;
            Value = value;
        }

        public override string Id {
            get { return "mana.outcome"; }
        }
        public override string NameText {
            get { return Name.GetText(SourceElement, Value); }
        }

        public override ElementType Element {
            get { return SourceElement; }
        }

        public override int BaseWeight {
            get { return Value * -50; }
        }

        public override Affect Clone() {
            return new ManaOutcome(SourceElement, Value);
        }

        public override void SetAffect() {
            base.SetAffect();
            LocalSummary.ChangeManaOutcome(SourceElement, Value);
            GlobalSummary.ChangeManaOutcome(SourceElement, Value);
        }
        public override void ResetAffect() {
            base.ResetAffect();
            LocalSummary.ChangeManaOutcome(SourceElement, -Value);
            GlobalSummary.ChangeManaOutcome(SourceElement, -Value);
        }
    }

    /// <summary>
    /// Эффект: блокировка энергии (маны)
    /// Параметры: тип энергии и блокируемые направления
    /// </summary>
    public class ManaLock : Affect {
        private static readonly ElementPhrase AffectLock = new ElementPhrase("affect.mana.lock");
        private static readonly Languages.Phrase AffectLockIncome = new Languages.Phrase("affect.mana.lock.income");
        private static readonly Languages.Phrase AffectLockOutcome = new Languages.Phrase("affect.mana.lock.outcome");

        public ElementType SourceElement;
        public bool LockIncome, LockOutcome;

        public ManaLock(ElementType element, bool income, bool outcome) {
            SourceElement = element;
            LockIncome = income;
            LockOutcome = outcome;
        }

        public override string Id {
            get { return "mana.lock"; }
        }
        public override Languages.Phrase Name {
            get { return LockIncome ? (LockOutcome ? AffectLock.GetPhrase(SourceElement) : AffectLockIncome) : (LockOutcome ? AffectLockOutcome : base.Name); }
        }
        public override string NameText {
            get { return Name.GetText(SourceElement.ManaName); }
        }

        public override ElementType Element {
            get { return SourceElement; }
        }

        public override Affect Clone() {
            return new ManaLock(SourceElement, LockIncome, LockOutcome);
        }

        public override void SetAffect() {
            base.SetAffect();
            GlobalSummary.SetManaLock(SourceElement, LockIncome, LockOutcome);
        }
        public override void ResetAffect() {
            base.ResetAffect();
            GlobalSummary.ResetManaLock(SourceElement, LockIncome, LockOutcome);
        }
    }

    /// <summary>
    /// Эффект: действие ragima на ману (уменьшение требований вдвое)
    /// Параметр: тип энергии
    /// </summary>
    public class RagimaMana : Affect, IManaCostChanger {
        private static readonly ElementPhrase Ragima = new ElementPhrase("affect.mana.ragima");

        public readonly ElementType RagimaElement;

        public RagimaMana(ElementType element) {
            RagimaElement = element;
        }

        public override string Id {
            get { return "mana.ragima"; }
        }
        public override Languages.Phrase Name {
            get { return Ragima.GetPhrase(RagimaElement); }
        }
        public override string NameText {
            get { return Name.GetText(RagimaElement); }
        }

        public override ElementType Element {
            get { return RagimaElement; }
        }

        public override int BaseWeight {
            get { return 500; }
        }

        public override bool Dispellable {
            get { return false; }
        }

        public override Affect Clone() {
            return new RagimaMana(RagimaElement);
        }

        public override void SetAffect() {
            base.SetAffect();
            GlobalSummary.ManaChangers.Add(this);
        }
        public override void ResetAffect() {
            base.ResetAffect();
            GlobalSummary.ManaChangers.Remove(this);
        }

        int IPrioritized.Priority {
            get { return 10; }
        }

        bool IManaCostChanger.Accept(Player player, Card card, ElementValues manas) {
            return manas[RagimaElement] > 0;
        }

        void IManaCostChanger.Change(Player player, Card card, ElementValues manas) {
            manas.Decrement(RagimaElement, manas[RagimaElement] / 2);
        }
    }

    public class WardMana: Affect, IManaCostChanger {
        public readonly CardType WardType;
        private bool _ended;

        public WardMana(CardType wardType) {
            WardType = wardType;
        }

        public override string Id {
            get { return "mana.ward"; }
        }

        public override int BaseWeight {
            get { return 500; }
        }

        public override bool IsEnded {
            get { return _ended; }
        }

        public override Affect Clone() {
            return new WardMana(WardType);
        }

        public override void SetAffect() {
            base.SetAffect();
            GlobalSummary.ManaChangers.Add(this);
            GlobalSummary.OnStartOffencePhase += DoTurn;
        }
        public override void ResetAffect() {
            base.ResetAffect();
            GlobalSummary.ManaChangers.Remove(this);
            GlobalSummary.OnStartOffencePhase -= DoTurn;
        }

        private void DoTurn() {
            if (Owner.Owner.Meditation > 0)
                Owner.Owner.DecMeditation(1);
            else
                _ended = true;
        }

        int IPrioritized.Priority {
            get { return 8; }
        }

        bool IManaCostChanger.Accept(Player player, Card card, ElementValues manas) {
            return card.CardType == WardType;
        }

        void IManaCostChanger.Change(Player player, Card card, ElementValues manas) {
            manas.Increment(card.Element);
            foreach (ElementType element in ElementType.AllElements)
                manas.Increment(element, manas[element]);
        }
    }
}
